<?php
/**
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.niushop.com
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 */

namespace addon\fenxiao\admin\controller;

use addon\fenxiao\model\FenxiaoAccount;
use addon\fenxiao\model\FenxiaoApply;
use addon\fenxiao\model\FenxiaoData;
use addon\fenxiao\model\FenxiaoLevel;
use addon\fenxiao\model\FenxiaoLevel as FenxiaoLevelModel;
use addon\fenxiao\model\FenxiaoOrder;
use app\admin\controller\BaseAdmin;
use addon\fenxiao\model\Fenxiao as FenxiaoModel;
use app\model\member\Member;
use app\model\system\Menu as MenuModel;
use addon\fenxiao\model\Config as ConfigModel;


/**
 *  分销设置
 */
class Fenxiao extends BaseAdmin
{
    /**
     * 分销概况
     */
    public function index()
    {
        //获取分销商账户统计
        $fenxiao_data_model = new FenxiaoData();
        $account_data = $fenxiao_data_model->getFenxiaoAccountData();
        $this->assign('account_data', $account_data);
        //累计佣金
        $fenxiao_account = number_format($account_data[ 'account' ] + $account_data[ 'account_withdraw' ], 2, '.', '');
        $this->assign('fenxiao_account', $fenxiao_account);
        //获取申请人数
        $fenxiao_apply_num = $fenxiao_data_model->getFenxiaoApplyCount();
        $this->assign('fenxiao_apply_num', $fenxiao_apply_num);
        //分销商人数
        $fenxiao_num = $fenxiao_data_model->getFenxiaoCount();
        $this->assign('fenxiao_num', $fenxiao_num);

        //推广订单数
        $hotel_recommend_order_count = model('hotel_fenxiao_order')->getCount();
        $recommend_order_count = model('fenxiao_order')->getCount();
        $total_recommend_order_count = $recommend_order_count+$hotel_recommend_order_count;
        $this->assign('total_recommend_order_count', $total_recommend_order_count);

        //产生的佣金
        $hotel_recommend_order_one_commission = model('hotel_fenxiao_order')->getSum([],'one_commission');
        $hotel_recommend_order_two_commission = model('hotel_fenxiao_order')->getSum([],'two_commission');
        $recommend_order_one_commission = model('fenxiao_order')->getSum([],'one_commission');
        $recommend_order_two_commission = model('fenxiao_order')->getSum([],'two_commission');
        $total_recommend_order_commission = $hotel_recommend_order_one_commission+$hotel_recommend_order_two_commission+$recommend_order_one_commission+$recommend_order_two_commission;
        $this->assign('total_recommend_order_commission', $total_recommend_order_commission);

        //推广人数
        $total_recommend_people = model('member')->getCount([['source_member','>',0]]);
        $this->assign('total_recommend_people', $total_recommend_people);
        return $this->fetch('fenxiao/index');
    }

    /**
     * 添加分销商
     */
    public function add()
    {
        if (request()->isAjax()) {

            $fenxiao_data = [
                'fenxiao_name' => input('fenxiao_name',''),//分销商名称
                'level_id'     => input('level_id',0),//分销商登记
                'parent'       => input('fenxiao_id',0),//上级分销商ID
                'member_id'    => input('member_id',0),//关联会员ID
            ];
            $apply_model = new FenxiaoApply();
            return $apply_model->addFenxiao($fenxiao_data);

        } else {

            //获取分销商等级
            $level_model = new FenxiaoLevelModel();
            $level_list  = $level_model->getLevelList([['status', '=', 1]], 'level_id,level_name');
            $this->assign('level_list', $level_list['data']);

            //获取分销商列表
            $fenxiao_model = new FenxiaoModel();
            $condition[]   = ['status','=','1'];
            $fenxiao_list  = $fenxiao_model->getFenxiaoList($condition, 'fenxiao_id,fenxiao_name');
            $this->assign('fenxiao_list', $fenxiao_list['data']);

            //获取会员列表
            $member_model = new Member();
            $where[] = ['is_fenxiao','=','0'];
            $member_list = $member_model->getMemberList($where,'member_id,nickname');
            $this->assign('member_list', $member_list['data']);

            return $this->fetch('fenxiao/add');
        }

    }

    /**
     * 分销商列表
     */
    public function lists()
    {
        $model = new FenxiaoModel();
        if (request()->isAjax()) {

            $condition = [];

            $fenxiao_name = input('fenxiao_name', '');
            if ($fenxiao_name) {
                $condition[] = [ 'f.fenxiao_name', 'like', '%' . $fenxiao_name . '%' ];
            }

            $parent_name = input('parent_name', '');
            if ($parent_name) {
                $condition[] = [ 'pf.fenxiao_name', 'like', '%' . $parent_name . '%' ];
            }

            $mobile = input('mobile', '');
            if ($mobile) {
                $condition[] = [ 'm.mobile', 'like', '%' . $mobile . '%' ];
            }

            $level_id = input('level_id', '');
            if ($level_id) {
                $condition[] = [ 'f.level_id', '=', $level_id ];
            }
            $start_time = input('start_time', '');
            $end_time = input('end_time', '');
            if ($start_time && $end_time) {
                $condition[] = [ 'f.create_time', 'between', [ date_to_time($start_time), date_to_time($end_time) ] ];
            } elseif (!$start_time && $end_time) {
                $condition[] = [ 'f.create_time', '<=', date_to_time($end_time) ];

            } elseif ($start_time && !$end_time) {
                $condition[] = [ 'f.create_time', '>=', date_to_time($start_time) ];
            }

            $status = input('status', '');
            if (!empty($status)) {
                $condition[] = [ 'f.status', '=', $status ];
            }
            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);
            $list = $model->getFenxiaoPageList($condition, $page, $page_size, 'f.create_time desc');
            foreach ($list['data']['list'] as $k=>$v){
                $withdraw_count = model('member_withdraw')->getCount(['member_id'=>$v['member_id'],'status'=>2]);
                $list['data']['list'][$k]['withdraw_count'] = $withdraw_count;
                $hotel_fenxiao_order_money = model('hotel_fenxiao_order')->getSum(['one_fenxiao_id'=>$v['fenxiao_id']],'real_room_money');
                $list['data']['list'][$k]['hotel_fenxiao_order_money'] = $hotel_fenxiao_order_money;
            }
            return $list;

        } else {
            $level_model = new FenxiaoLevel();
            $level_list = $level_model->getLevelList([ [ 'status', '=', 1 ] ], 'level_id,level_name');
            $this->assign('level_list', $level_list[ 'data' ]);

            $config_model = new ConfigModel();
            $basics = $config_model->getFenxiaoBasicsConfig();
            $this->assign("basics_info", $basics[ 'data' ][ 'value' ]);

            //获取分销商账户统计
            $fenxiao_data_model = new FenxiaoData();
            $account_data = $fenxiao_data_model->getFenxiaoAccountData();
            $this->assign('account_data', $account_data);
            //累计佣金
            $fenxiao_account = number_format($account_data[ 'account' ] + $account_data[ 'account_withdraw' ], 2, '.', '');
            $this->assign('fenxiao_account', $fenxiao_account);
            //获取申请人数
            $fenxiao_apply_num = $fenxiao_data_model->getFenxiaoApplyCount();
            $this->assign('fenxiao_apply_num', $fenxiao_apply_num);
            //分销商人数
            $fenxiao_num = $fenxiao_data_model->getFenxiaoCount();
            $this->assign('fenxiao_num', $fenxiao_num);

            //推广订单数
            $hotel_recommend_order_count = model('hotel_fenxiao_order')->getCount();
            $recommend_order_count = model('fenxiao_order')->getCount();
            $total_recommend_order_count = $recommend_order_count+$hotel_recommend_order_count;
            $this->assign('total_recommend_order_count', $total_recommend_order_count);

            //产生的佣金
            $hotel_recommend_order_one_commission = model('hotel_fenxiao_order')->getSum([],'one_commission');
            $hotel_recommend_order_two_commission = model('hotel_fenxiao_order')->getSum([],'two_commission');
            $recommend_order_one_commission = model('fenxiao_order')->getSum([],'one_commission');
            $recommend_order_two_commission = model('fenxiao_order')->getSum([],'two_commission');
            $total_recommend_order_commission = $hotel_recommend_order_one_commission+$hotel_recommend_order_two_commission+$recommend_order_one_commission+$recommend_order_two_commission;
            $this->assign('total_recommend_order_commission', $total_recommend_order_commission);

            //推广人数
            $total_recommend_people = model('member')->getCount([['source_member','>',0]]);
            $this->assign('total_recommend_people', $total_recommend_people);

            //已提现金额
            $balance_withdraw = model('member')->getSum([],'balance_withdraw');
            //未提现金额
            $balance_money = model('member')->getSum([],'balance_money');
            $this->assign('balance_withdraw', $balance_withdraw);
            $this->assign('balance_money', $balance_money);
            $this->forthMenu();
            return $this->fetch('fenxiao/lists');
        }
    }

    /**
     * 详情
     */
    public function detail()
    {
        $fenxiao_id = input('fenxiao_id', '');
        $model = new FenxiaoModel();
        $fenxiao_leve_model = new FenxiaoLevelModel();
        $condition[] = [ 'f.fenxiao_id', '=', $fenxiao_id ];
        $info = $model->getFenxiaoDetailInfo($condition);
        $fenxiao_level = $fenxiao_leve_model->getLevelInfo([ [ 'level_id', '=', $info[ 'data' ][ 'level_id' ] ] ]);
        $this->assign('status', $model->fenxiao_status_zh);
        $this->assign('level', $fenxiao_level[ 'data' ]);
        $this->assign('info', $info[ 'data' ]);

        $this->fiveMenu([ 'fenxiao_id' => $fenxiao_id ]);
        return $this->fetch('fenxiao/fenxiao_detail');
    }

    /**
     * 分销账户信息
     */
    public function account()
    {
        $model = new FenxiaoModel();
        $fenxiao_id = input('fenxiao_id', '');

        $condition[] = [ 'f.fenxiao_id', '=', $fenxiao_id ];
        $info = $model->getFenxiaoDetailInfo($condition);
        $account = $info[ 'data' ][ 'account' ] - $info[ 'data' ][ 'account_withdraw_apply' ];
        $info[ 'data' ][ 'account' ] = number_format($account, 2, '.', '');
        $this->assign('fenxiao_info', $info[ 'data' ]);

        if (request()->isAjax()) {

            $account_model = new FenxiaoAccount();
            $page = input('page', 1);
            $status = input('status', '');

            $fenxiao_id = input('fenxiao_id', '');
            $list_condition[] = [ 'fenxiao_id', '=', $fenxiao_id ];
            if ($status) {
                if ($status == 1) {
                    $list_condition[] = [ 'money', '>', 0 ];
                } else {
                    $list_condition[] = [ 'money', '<', 0 ];
                }
            }

            $start_time = input('start_time', '');
            $end_time = input('end_time', '');
            if ($start_time && $end_time) {
                $list_condition[] = [ 'create_time', 'between', [ $start_time, $end_time ] ];
            } elseif (!$start_time && $end_time) {
                $list_condition[] = [ 'create_time', '<=', $end_time ];

            } elseif ($start_time && !$end_time) {
                $list_condition[] = [ 'create_time', '>=', $start_time ];
            }

            $page_size = input('page_size', PAGE_LIST_ROWS);
            $list = $account_model->getFenxiaoAccountPageList($list_condition, $page, $page_size);
            return $list;
        }
        $this->assign('fenxiao_id', $fenxiao_id);

        $this->fiveMenu([ 'fenxiao_id' => $fenxiao_id ]);
        return $this->fetch('fenxiao/fenxiao_account');
    }

    /**
     * 订单管理
     */
    public function order()
    {
        $model = new FenxiaoOrder();

        $fenxiao_id = input('fenxiao_id', '');
        if (request()->isAjax()) {

            $page_index = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);
            $fenxiao_id = input('fenxiao_id', '');

            $condition[] = [ 'one_fenxiao_id|two_fenxiao_id|three_fenxiao_id', '=', $fenxiao_id ];

            $search_text_type = input('search_text_type', "goods_name");//订单编号/店铺名称/商品名称
            $search_text = input('search_text', "");
            if (!empty($search_text)) {
                $condition[] = [ 'fo.' . $search_text_type, 'like', '%' . $search_text . '%' ];
            }

            //下单时间
            $start_time = input('start_time', '');
            $end_time = input('end_time', '');
            if (!empty($start_time) && empty($end_time)) {
                $condition[] = [ 'o.create_time', '>=', date_to_time($start_time) ];
            } elseif (empty($start_time) && !empty($end_time)) {
                $condition[] = [ 'o.create_time', '<=', date_to_time($end_time) ];
            } elseif (!empty($start_time) && !empty(date_to_time($end_time))) {
                $condition[] = [ 'o.create_time', 'between', [ date_to_time($start_time), date_to_time($end_time) ] ];
            }

            $list = $model->getFenxiaoOrderPageList($condition, $page_index, $page_size);
            return $list;

        } else {
            //订单状态
            $this->assign('fenxiao_id', $fenxiao_id);
            $this->fiveMenu([ 'fenxiao_id' => $fenxiao_id ]);
            return $this->fetch('fenxiao/order_lists');
        }
    }

    /**
     * 订单详情
     */
    public function orderDetail()
    {
        $fenxiao_order_model = new FenxiaoOrder();
        $fenxiao_order_id = input('fenxiao_order_id', '');
        $order_info = $fenxiao_order_model->getFenxiaoOrderDetail([ [ 'fenxiao_order_id', '=', $fenxiao_order_id ] ]);
        $this->assign('order_info', $order_info[ 'data' ]);
        return $this->fetch('fenxiao/order_detail');
    }

    /**
     * 冻结
     */
    public function frozen()
    {
        $fenxiao_id = input('fenxiao_id', '');

        $model = new FenxiaoModel();

        return $model->frozen($fenxiao_id);
    }

    /**
     * 恢复正常
     */
    public function unfrozen()
    {
        $fenxiao_id = input('fenxiao_id', '');

        $model = new FenxiaoModel();

        return $model->unfrozen($fenxiao_id);
    }


    /**
     * 分销商申请列表
     */
    public function apply()
    {
        $model = new FenxiaoApply();
        if (request()->isAjax()) {

            $condition[] = [ 'status', '=', 1 ];

            $fenxiao_name = input('fenxiao_name', '');
            if ($fenxiao_name) {
                $condition[] = [ 'fenxiao_name', 'like', '%' . $fenxiao_name . '%' ];
            }
            $mobile = input('mobile', '');
            if ($mobile) {
                $condition[] = [ 'mobile', 'like', '%' . $mobile . '%' ];
            }
            $level_id = input('level_id', '');
            if ($level_id) {
                $condition[] = [ 'level_id', '=', $level_id ];
            }
            $create_start_time = input('create_start_time', '');
            $create_end_time = input('create_end_time', '');
            if ($create_start_time && $create_end_time) {
                $condition[] = [ 'create_time', 'between', [ strtotime($create_start_time), strtotime($create_end_time) ] ];
            } elseif (!$create_start_time && $create_end_time) {
                $condition[] = [ 'create_time', '<=', strtotime($create_end_time) ];

            } elseif ($create_start_time && !$create_end_time) {
                $condition[] = [ 'create_time', '>=', strtotime($create_start_time) ];
            }

            $rg_start_time = input('rg_start_time', '');
            $rg_end_time = input('rg_end_time', '');
            if ($rg_start_time && $rg_end_time) {
                $condition[] = [ 'reg_time', 'between', [ strtotime($rg_start_time), strtotime($rg_end_time) ] ];
            } elseif (!$rg_start_time && $rg_end_time) {
                $condition[] = [ 'reg_time', '<=', strtotime($rg_end_time) ];

            } elseif ($rg_start_time && !$rg_end_time) {
                $condition[] = [ 'reg_time', '>=', strtotime($rg_start_time) ];
            }

            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);
            $list = $model->getFenxiaoApplyPageList($condition, $page, $page_size, 'create_time desc', '*');
            return $list;

        } else {

            $level_model = new FenxiaoLevel();
            $level_list = $level_model->getLevelList([ [ 'status', '=', 1 ] ], 'level_id,level_name');
            $this->assign('level_list', $level_list[ 'data' ]);

            $this->forthMenu();
            return $this->fetch('fenxiao/apply');
        }
    }

    /**
     * 分销商申请通过
     */
    public function applyPass()
    {
        $apply_id = input('apply_id');

        $model = new FenxiaoApply();
        $res = $model->pass($apply_id);
        return $res;
    }

    /**
     * 分销商申请通过
     */
    public function applyRefuse()
    {
        $apply_id = input('apply_id');

        $model = new FenxiaoApply();
        $res = $model->refuse($apply_id);
        return $res;
    }


    /**
     * 四级菜单
     * @param unknown $params
     */
    protected function fiveMenu($params = [])
    {
        $url = strtolower($this->url);
        $menu_model = new MenuModel();
        $menu_info = $menu_model->getMenuInfo([ [ 'url', "=", $url ], [ 'level', '=', 5 ] ], 'parent');

        if (!empty($menu_info[ 'data' ])) {
            $menus = $menu_model->getMenuList([ [ 'app_module', "=", $this->app_module ], [ 'is_show', "=", 1 ], [ 'parent', '=', $menu_info[ 'data' ][ 'parent' ] ] ], '*', 'sort asc');
            foreach ($menus[ 'data' ] as $k => $v) {
                $menus[ 'data' ][ $k ][ 'parse_url' ] = addon_url($menus[ 'data' ][ $k ][ 'url' ], $params);
                if ($menus[ 'data' ][ $k ][ 'url' ] == $url) {
                    $menus[ 'data' ][ $k ][ 'selected' ] = 1;
                } else {
                    $menus[ 'data' ][ $k ][ 'selected' ] = 0;
                }
            }
            $this->assign('forth_menu', $menus[ 'data' ]);
        }
    }

    /**
     * 分销商下级团队
     */
    public function team()
    {
        $fenxiao_id = input('fenxiao_id', 0);
        $fenxiao_model = new FenxiaoModel();
        if (request()->isAjax()) {

            $condition[] = [ 'status', '=', 1 ];

            $level = input('level', 1);

            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);
            $list = $fenxiao_model->getFenxiaoTeam($level, $fenxiao_id, $page, $page_size);
            return $list;

        } else {
            $this->assign('fenxiao_id', $fenxiao_id);
            $this->fiveMenu([ 'fenxiao_id' => $fenxiao_id ]);
            return $this->fetch('fenxiao/team');
        }
    }


    /**
     * 变更上下级
     */
    public function change()
    {
        $member_id = input('member_id');

        $model = new FenxiaoModel();
        if (request()->isAjax()) {

            $member_model = new Member();
            $member_info_result = $member_model->getMemberInfo([ [ 'member_id', '=', $member_id ] ], 'fenxiao_id,is_fenxiao');
            $member_info = $member_info_result[ 'data' ];
            if ($member_info[ 'is_fenxiao' ] == 1) {
                $condition[] = [ 'f.fenxiao_id', '<>', $member_info[ 'fenxiao_id' ] ];
                $condition[] = [ 'f.parent', '<>', $member_info[ 'fenxiao_id' ] ];
            }

            $condition[] = [ 'f.status', '=', 1 ];

            $fenxiao_name = input('fenxiao_name', '');
            if ($fenxiao_name) {
                $condition[] = [ 'f.fenxiao_name', 'like', '%' . $fenxiao_name . '%' ];
            }

            $parent_name = input('parent_name', '');
            if ($parent_name) {
                $condition[] = [ 'pf.fenxiao_name', 'like', '%' . $parent_name . '%' ];
            }

            $level_id = input('level_id', '');
            if ($level_id) {
                $condition[] = [ 'f.level_id', '=', $level_id ];
            }
            $start_time = input('start_time', '');
            $end_time = input('end_time', '');
            if ($start_time && $end_time) {
                $condition[] = [ 'f.create_time', 'between', [ date_to_time($start_time), date_to_time($end_time) ] ];
            } elseif (!$start_time && $end_time) {
                $condition[] = [ 'f.create_time', '<=', date_to_time($end_time) ];

            } elseif ($start_time && !$end_time) {
                $condition[] = [ 'f.create_time', '>=', date_to_time($start_time) ];
            }

            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);
            $list = $model->getFenxiaoPageList($condition, $page, $page_size, 'f.create_time desc');
            return $list;

        } else {
            $level_model = new FenxiaoLevelModel();
            $level_list = $level_model->getLevelList([ [ 'status', '=', 1 ] ], 'level_id,level_name');
            $this->assign('level_list', $level_list[ 'data' ]);

            $this->assign('member_id', $member_id);
            return $this->fetch('fenxiao/change');
        }
    }

    /**
     * 确认变更
     */
    public function confirmChange()
    {
        if (request()->isAjax()) {

            $member_id = input('member_id', '');
            $parent = input('parent', '');
            $model = new FenxiaoModel();

            $res = $model->changeParentFenxiao($member_id, $parent);
            return $res;
        }
    }
}